package it.cqupt.web.controller.system;

import com.github.pagehelper.PageInfo;
import it.cqupt.domain.system.Dept;
import it.cqupt.domain.system.Module;
import it.cqupt.domain.system.Role;
import it.cqupt.domain.system.User;
import it.cqupt.service.system.UserService;
import it.cqupt.utils.BeanUtil;
import it.cqupt.web.controller.BaseServlet;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/system/user")
public class UserServlet extends BaseServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String operation = req.getParameter("operation");
        if("list".equals(operation)){
            this.list(req,resp);
        }else if("toAdd".equals(operation)){
            this.toAdd(req,resp);
        }else if("save".equals(operation)){
            this.save(req,resp);
        }else if("toEdit".equals(operation)){
            this.toEdit(req,resp);
        }else if("edit".equals(operation)){
            this.edit(req,resp);
        }else if("delete".equals(operation)){
            this.delete(req,resp);
        }else if("userRoleList".equals(operation)){
            this.userRoleList(req,resp);
        }else if("updateRole".equals(operation)){
            this.updateRole(req,resp);
        }else if("login".equals(operation)){
            this.login(req,resp);
        }else if("logout".equals(operation)){
            this.logout(req,resp);
        }
    }

    private void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String email = req.getParameter("email");
        String password = req.getParameter("password");
        User user = us.login(email,password);
        if(user != null){
            req.getSession().setAttribute("loginUser",user);
            List<Module> moduleList = us.findModuleListByUid(user.getId());
            StringBuffer sb = new StringBuffer();
            for(Module module : moduleList){
                sb.append(module.getCurl());
                sb.append(",");
            }
            req.getSession().setAttribute("authorStr",sb.toString());
            req.setAttribute("moduleList",moduleList);
            req.getRequestDispatcher("/WEB-INF/pages/home/main.jsp").forward(req,resp);
        }else{
            req.setAttribute("error","邮箱或密码错误，请重新输入");
            resp.sendRedirect(req.getContextPath()+"/login.jsp");
        }
    }
    public void logout(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getSession().removeAttribute("loginUser");
        req.getSession().removeAttribute("authorStr");
        resp.sendRedirect(req.getContextPath()+"/login.jsp");
    }

    private void updateRole(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String[] roleIds = req.getParameterValues("roleIds");
        String userId = req.getParameter("userId");
        us.updateRole(userId,roleIds);
        this.list(req,resp);
    }

    private void userRoleList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userId = req.getParameter("id");
        User byId = us.findById(userId);
        List<Role> all = roleService.findAllUserRole(userId);
        req.setAttribute("roleList",all);
        req.setAttribute("user",byId);
        req.getRequestDispatcher("/WEB-INF/pages/system/user/role.jsp").forward(req,resp);
    }


    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        us.delete(BeanUtil.fillBean(req,User.class,"yyyy-MM-dd"));
        resp.sendRedirect(req.getContextPath()+"/system/user?operation=list");
    }

    private void edit(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        User user = BeanUtil.fillBean(req, User.class,"yyyy-MM-dd");
        us.update(user);
        resp.sendRedirect(req.getContextPath()+"/system/user?operation=list");
    }
//
    private void toEdit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        User byId = us.findById(id);
        List<Dept> all = ds.findAll();
        req.setAttribute("deptList",all);
        req.setAttribute("user",byId);
        req.getRequestDispatcher("/WEB-INF/pages/system/user/update.jsp").forward(req,resp);

    }

    private void save(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        User user = BeanUtil.fillBean(req, User.class,"yyyy-MM-dd");
        us.save(user);
        resp.sendRedirect(req.getContextPath()+"/system/user?operation=list");
    }

    private void toAdd(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        List<Dept> depts = ds.findAll();
        req.setAttribute("deptList",depts);
        req.getRequestDispatcher("/WEB-INF/pages/system/user/add.jsp").forward(req,resp);
    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int page = 1;
        int size = 5;
        if(StringUtils.isNotBlank(req.getParameter("page"))){
            page = Integer.parseInt(req.getParameter("page"));
        }
        if(StringUtils.isNotBlank(req.getParameter("size"))){
            size = Integer.parseInt(req.getParameter("size"));
        }
        PageInfo pageInfo = us.findAll(page,size);

        req.setAttribute("page",pageInfo);
        req.getRequestDispatcher("/WEB-INF/pages/system/user/list.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
